package ws.jdbc.concurrent.driver.sqlserver.cursor.wrapper;

import java.sql.*;

/**
 * Implementation of the {@link ICursorScrollUpdate} services.
 * 
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class CursorScrollUpdate extends CursorScrollReadOnly implements ResultSet {

  public CursorScrollUpdate(ResultSet rs) {
    super(rs);
  }

  @Override
  public void moveToInsertRow() throws SQLException {
    lock.lock();
    currentCursor = id;
    rs.moveToInsertRow();
  }

  @Override
  public void moveToCurrentRow() throws SQLException {
    lock.lock();
    try {
      restoreContext();
      currentCursor = id;
    } finally {
      unlockUpdate();
    }
  }

  @Override
  public void cancelRowUpdates() throws SQLException {
    lock.lock();
    try {
      currentCursor = id;
      rs.cancelRowUpdates();
    } finally {
      unlockUpdate();
    }
  }

  @Override
  public void updateDouble(int columnIndex, double x) throws SQLException {
    lock.lock();
    restoreContext();
    currentCursor = id;
    rs.updateDouble(columnIndex, x);
  }

  @Override
  public void updateDate(int columnIndex, Date x) throws SQLException {
    lock.lock();
    restoreContext();
    currentCursor = id;
    rs.updateDate(columnIndex, x);
  }

  @Override
  public void updateInt(int columnIndex, int x) throws SQLException {
    lock.lock();
    restoreContext();
    currentCursor = id;
    rs.updateInt(columnIndex, x);
  }

  @Override
  public void updateString(int columnIndex, String x) throws SQLException {
    lock.lock();
    restoreContext();
    currentCursor = id;
    rs.updateString(columnIndex, x);
  }

  @Override
  public void deleteRow() throws SQLException {
    lock.lock();
    try {
      restoreContext();
      currentCursor = id;
      rs.deleteRow();
    } finally {
      lock.unlock();
    }
  }

  @Override
  public void updateRow() throws SQLException {
    lock.lock();
    currentCursor = id;
    rs.updateRow();
    unlockUpdate();
  }

  @Override
  public void insertRow() throws SQLException {
    lock.lock();
    currentCursor = id;
    rs.insertRow();
    unlockUpdate();
  }
}
